import { EventManager } from "../Managers/EventManager";
import { DelayFramer } from "../Ticks/DelayFramer";
import { Fun } from "../Utility/dx/Fun";

const { ccclass, menu } = cc._decorator;
@ccclass
@menu("Framework/基础组件/FView")
export default class FView extends cc.Component {
    public show(p: cc.Node, data?: object): void {
        if (p != null && this.node != null) {
            if (p != this.node.parent) {
                this.node.removeFromParent();
                p.addChild(this.node);
            }
        }
    }

    public hide(data?: object): void {
        if (this.node)
            this.node.removeFromParent();
    }
    /**添加事件**/
    protected addEvents(): void {
    }
    /**移除事件**/
    protected removeEvents(): void {
        EventManager.removeEvent(this);
    }
    /**启动延时处理**/
    protected invalidate(): void {
        DelayFramer.Push(Fun(this.render, this));//this, this.render);
    }
    /**立即进行延时处理**/
    protected onInvalidate(): void {
        DelayFramer.Remove(Fun(this.render, this));
        this.render();
    }
    /**延时处理方法**/
    protected render(): void {

    }
    onEnable() {
        this.addEvents();
    }
    onDisable() {
        this.removeEvents();
    }

    /**
     * 
     * @param com 需要找的组件
     * @param child 需要找的子节点，可以找多层
     */
    protected getComByChild<T extends cc.Component>(com: { prototype: T }, ...child: string[]): T {
        let n: cc.Node = this.node;
        for (let i: number = 0; i < child.length; i++) {
            n = n.getChildByName(child[i]);
        }
        return n.getComponent(com);
    }

    /**
     * 直接通过名称获取子节点
     * @param name 子节点名称，可变长参数
     */
    protected getChildByName(...name: string[]): cc.Node {
        
        let n: cc.Node = this.node;
        name.forEach(s => n = n.getChildByName(s));
        return n;
    }
}
